import QtQuick 1.1
import com.nokia.meego 1.0

import "storage.js" as Database
import "qbconst.js" as Util

Page {
    id: userPage
    tools:ToolBarLayout {
          id: commonTools
          visible: true
          ToolIcon {
              platformIconId: "toolbar-back"

              onClicked:  appWindow.pageStack.pop();
          }
      }

//    onStatusChanged: {
//        if (status===PageStatus.Activating && userLoader.sourceComponent === undefined)
//           { userLoader.sourceComponent = userComp
//            console.log("1111111111111111111111");
//            }

//        if (status===PageStatus.Deactivating)
//           { userLoader.sourceComponent = undefined
//             console.log("11111111111111111111122221");
//        }
//    }


    Loader {
        id: userLoader
        sourceComponent:userComp
        anchors {
            fill: parent
            topMargin: 30

        }
    }
    Component {
        id: userComp

            Column {
                id: contCol
               // y: platformStyle.paddingMedium
                width: parent.width
                Loader {
                    width: parent.width
                    sourceComponent: appWindow.qbtoken!=="" ? loggedComp : unLogComp
                }
                Loader {
                    width: parent.width
                    visible: appWindow.qbtoken !== ""
                   // sourceComponent: visible ? myArticleComp : undefined
                    sourceComponent:undefined
                }
                Row{
                ListItemQ {
                    role: "foot"
                    text: "退出"
                    onClicked: Qt.quit()
                }
               }
                Component {
                    id: unLogComp
                    Row{

                    ListItemQ {
                        role: "top"
                        text: "点这登录可获得更多功能"
                        onClicked: appWindow.pageStack.push(Qt.resolvedUrl("Login.qml"))
                    }
                    }
                }
                Component {
                    id: loggedComp
                    Row{
                    ListItemQ {
                        role: "top"
                        onClicked: diag.open()
                        Item {
                            id: avatarItem
                            anchors { left: parent.paddingItem.left;
                                    verticalCenter: parent.verticalCenter }
                            height: parent.paddingItem.height; width: height
                            Image {
                                id: avatarThumb
                                anchors.fill: parent
                                source: visible ? "images/thumb_avatar.png" : ""
                            }
                            Image {
                                anchors.fill: parent
                                fillMode: Image.PreserveAspectFit
                                source: {
                                    var obj = JSON.parse(Database.getCache("userData"))
                                    return Util.getAvatar(obj.user.icon, obj.user.id)
                                }
                                onStatusChanged: if(status==Image.Ready){
                                                     avatarThumb.visible = false
                                                 }
                            }
                        }

                        Label {
                            anchors { left: avatarItem.right; leftMargin: 5;
                                right: parent.paddingItem.right; verticalCenter: parent.verticalCenter }
                            elide: Text.ElideRight
                            text: JSON.parse(Database.getCache("userData")).user.login

                            color: appWindow.colorDark
                        }
                        Image {
                            anchors { right: parent.paddingItem.right; verticalCenter: parent.verticalCenter }
                            source: "images/icon_edit_enable.png"
                            height: children.height
                            fillMode: Image.PreserveAspectFit
                            smooth: true
                        }
                    }
                }
                       }
                Component {
                    id: myArticleComp
                    Row{
                    ListItemQ {
                        text: "我的糗事"
                        showIndicator: true
                        onClicked: appWindow.pageStack.push(Qt.resolvedUrl("MyArticlePage.qml",
                                                                           {firsturl:"http://m2.qiushibaike.com/user/my/articles?page=1&count=30"}
                                                                           ))
                    }
                    }
                }
            }
        }



    SelectionDialog {
        id: diag
        titleText: appWindow.qbtoken == "" ? "" : JSON.parse(Database.getCache("userData")).user.login
        model: ["退出登录","取消"]
        onAccepted: {
            if(selectedIndex==0){
                Database.setSetting("qbtoken","")
                Database.clearCache("userData")
                appWindow.qbtoken = ""
            }
        }
    }
}
